package csc.fresher.finalgroupfour.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Customer;
import csc.fresher.finalgroupfour.domain.User;
import csc.fresher.finalgroupfour.service.BankService;



@Controller
public class ChangeInfoController {
	private BankService bankService;

	public BankService getBankService() {
		return bankService;
	}

	@Autowired
	public void setBankService(BankService bankService) {
		this.bankService = bankService;
	}

	@RequestMapping(value = "/profile")
	public String changeInfo(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	      String loginId = auth.getName(); //get logged in Id
	      User user = bankService.findUser(loginId);
	     session.setAttribute("user", user);
	     List<Account> accountList = (List<Account>) user.getAccounts();
	     session.setAttribute("accountList", accountList);
	     Customer customer = accountList.get(0).getCustomer();
	     session.setAttribute("customer", customer);
	     System.out.println(customer.getAddress1());
		return ("profile");
	}

	@RequestMapping(value = "/checkInfo")
	public String getProfile(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		String lastName = request.getParameter("lastName");
		String firstName = request.getParameter("firstName");
		String midName = request.getParameter("midName");
		String email1 = request.getParameter("email1");
		String email2 = request.getParameter("email2");
		String address1 = request.getParameter("address1");
		String address2 = request.getParameter("address2");
		String mobileNumber = request.getParameter("mobileNumber");
		User user = (User) session.getAttribute("user");
		Customer customer = (Customer) session.getAttribute("customer");
		
		customer.setFirstName(firstName);
		customer.setLastName(lastName);
		customer.setMidName(midName);
		customer.setEmail1(email1);
		customer.setEmail2(email2);
		customer.setAddress1(address1);
		customer.setAddress2(address2);
		customer.setMobileNumber(mobileNumber);
		
		bankService.updateCustomer(customer);

		return ("profile");
	}

	@RequestMapping(value = "/changePass")
	public String changePass(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		String newpass = request.getParameter("newpass");
		
		User user = (User) session.getAttribute("user");
		user.setPassword(newpass);
		bankService.updateUser(user);

		return ("profile");
	}
}
